package com.WISSC.serviceImpl;

import com.WISSC.bean.*;
import com.WISSC.bean.layuiBean.ResultPage;
import com.WISSC.mapper.OccupationMapper;
import com.WISSC.service.OccupationImageService;
import com.WISSC.service.OccupationService;
import com.WISSC.util.Respons.ResponseUtil;
import com.WISSC.util.date.DateUtil;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpSession;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 职业资格信息表 服务实现类
 * </p>
 *
 * @author 张凯
 * @since 2019-05-06
 */
@Service
public class OccupationServiceImpl extends ServiceImpl<OccupationMapper, Occupation> implements OccupationService {


    @Autowired
    OccupationImageService occupationImageService;

    /**
     * @param current
     * @param size
     * @param idNo
     * @param idNumber
     * @param regTime
     * @param city
     * @param qualifications
     * @param subject
     * @param subjectLevel
     * @param name
     * @return 查询 + 分页
     */
    @Override
    public ResultPage<Occupation> selectMyPage(
            int current, int size, String idNo,
            String idNumber, Long regTime, String city,
            Integer qualifications, Integer subject, Integer subjectLevel,
            String name, HttpSession session) {
        Map<String, Object> paramMap = new HashMap<>();
        paramMap.put("page", (current - 1) * size);
        paramMap.put("limit", size);
        paramMap.put("idNo", idNo);
        paramMap.put("name", name);
        paramMap.put("idNumber", idNumber);
        paramMap.put("regTime", regTime);
        paramMap.put("city", city);
        paramMap.put("subject", subject);
        paramMap.put("subjectLevel", subjectLevel);
        paramMap.put("qualifications", qualifications);
        User user = (User) session.getAttribute("user");
        if (user.getUsername().equals("admin")) {
            paramMap.put("type", "");
        } else {
            paramMap.put("type", 1);
        }
        List<Occupation> list = baseMapper.selectOccupation(paramMap);
        Integer count = baseMapper.countOccupation(paramMap);
        ResultPage<Occupation> resultPage = new ResultPage<>();
        resultPage.setCode(0);
        resultPage.setCount(count);
        resultPage.setMsg("成功");
        resultPage.setData(list);
        return resultPage;
    }

    /**
     * @param occupation
     * @return 信息添加
     */
    @Override
    public String occupationInsert(Occupation occupation, String param2) {
        Map<String, Object> map = new HashMap<String, Object>();
        String[] str = param2.split("-");
        ArrayList list = new ArrayList();
        for (int i = 0; i < str.length; i++) {
            list.add(str[i]);
        }

        occupation.setCreateTime(DateUtil.date());
        if (this.save(occupation)) {
            for (int i = 0; i < list.size(); i++) {
                String arr = (String) list.get(i);
                String[] ss = arr.split(",");
                if (ss != null) {
                    for (int j = 0; j < 1; j++) {
                        OccupationImage occupationImage = new OccupationImage();
                        if (ss[0].equals("undefined")) {
                            break;
                        }
                        occupationImage.setOccId(occupation.getId());
                        occupationImage.setSubject(Integer.parseInt(ss[0]));
                        occupationImage.setSubjectLevel(Integer.parseInt(ss[1]));
                        occupationImage.setCreateTime(DateUtil.date());
                        occupationImageService.save(occupationImage);
                    }
                } else {
                    map.put("flag", false);
                    map.put("msg", "添加失败");
                    return JSON.toJSONString(map);
                }
            }
        } else {
            map.put("flag", false);
            map.put("msg", "添加失败");
            return JSON.toJSONString(map);
        }
        map.put("flag", true);
        map.put("msg", "添加成功");
        return JSON.toJSONString(map);


    }


    /**
     * @param occupation
     * @param session1
     * @return 资格证书登陆
     */
    @Override
    public boolean login(Occupation occupation, OccupationImage occupationImage, HttpSession session1) {
        Map map = new HashMap<>();
        map.put("idNumber", occupation.getIdNumber());
        map.put("idNo", occupationImage.getIdNo());
        List<Occupation> users = baseMapper.login(map);
        //如果用户存在，放入session域
        if (users.size() > 0) {
            session1.setAttribute("occupation", users.get(0));
            return true;
        } else {
            return false;
        }
    }

    /**
     * @return 获取资格证书url
     */
    @Override
    public List<Occupation> getImageUrl(Map map) {
        List<Occupation> list = new ArrayList();
        //判断是身份证号 或 证件编号是否存在
        if (map.get("idNumber") != "" && map.get("idNumber") != null) {
            list = baseMapper.getOccupation(map);
        }
        if (map.get("idNo") != "" && map.get("idNo") != null && list.size() == 0) {
            Occupation occupation = baseMapper.getIdNumber((String) map.get("idNo"));
            map.put("idNumber",occupation.getIdNumber());
            list = baseMapper.getOccupation(map);
        }
        return list;
    }

    @Override
    public Map updateOccupation(Occupation occupation, String param2) {
        if (this.updateById(occupation)) {
            String[] split = param2.split("-");
            for (int i = 1; i < split.length; i++) {
                String[] split1 = split[i].split(",");
                for (int j = 0; j < 1; j++) {
                    if (split1[0].equals("undefined")) {
                        break;
                    }
                    OccupationImage occupationImage = new OccupationImage();
                    occupationImage.setId(Long.parseLong(split1[0]));
                    occupationImage.setSubject(Integer.parseInt(split1[1]));
                    occupationImage.setSubjectLevel(Integer.parseInt(split1[2]));
                    if (!occupationImageService.updateById(occupationImage)) {
                        return ResponseUtil.getExceptionMap();
                    }
                }
            }
            return ResponseUtil.getSuccessMap();
        }
        return ResponseUtil.getExceptionMap();
    }

    @Override
    public int getCount(Occupation occupation, int id) {

        if (occupation.getIdNumber() != "") {
            int count = baseMapper.getCount(occupation.getIdNumber(), id);
            return count;
        }
        if (occupation.getIdNo() != "") {
            Occupation o = baseMapper.getOccupationByidNo(occupation.getIdNo());
            int count = baseMapper.getCount(o.getIdNumber(), id);
            return count;
        }
        return 0;
    }

    @Override
    public int getOccupationCount(String idNumber, String idNo) {
        if (idNumber!=""){
            return baseMapper.getOccupationCount(idNumber);
        }
        Occupation occupation = baseMapper.getIdNumber(idNo);
        return baseMapper.getOccupationCount(occupation.getIdNumber());
    }

    @Override
    public List<Occupation> getImageUrl1(Map map) {
        List<Occupation> list = new ArrayList();
        //判断是身份证号 或 证件编号是否存在
        if (map.get("idNumber") != "" && map.get("idNumber") != null) {
            list = baseMapper.getOccupation1(map);
        }
        if (map.get("idNo") != "" && map.get("idNo") != null && list.size() == 0) {
            Occupation occupation = baseMapper.getIdNumber((String) map.get("idNo"));
            map.put("idNumber",occupation.getIdNumber());
            list = baseMapper.getOccupation1(map);
        }
        return list;
    }


    /**
     * @param occupation
     * @return 资格证书的图片编辑+ 资格证书
     */
//    @Override
//    public Map updateOccupationById(Occupation occupation) {
//        OccupationImage occupationImage = new OccupationImage();
//        occupationImage.setId(occupation.getImageId());
//        occupationImage.setImageUrl(occupation.getUrl());
//        occupationImageService.updateById(occupationImage);
//        if (this.updateById(occupation)) {
//            return ResponseUtil.getSuccessMap();
//        }
//        return ResponseUtil.getExceptionMap();
//    }

    /**
     * @param idNumber
     * @param
     * @return 个人赛事 资格证书查询
     */
    @Override
    public Occupation list(String idNumber) {

        return baseMapper.list(idNumber);
    }

    /**
     * @param id
     * @return 回显所在职业资格的所属地区
     */
    @Override
    public Map getOccupationCity(Long id) {
        String str = "";
        String sum = "";
        Map map = new HashMap();
        List<Occupation> list = baseMapper.getOccupationCity(id);
        for (int i = 0; i < list.size(); i++) {
            str = list.get(i).getCity();
            sum += list.get(i).getOccId() + "," + list.get(i).getSubject() + "," + list.get(i).getSubjectLevel() + "-";
        }
        System.out.println("city" + str);
        map.put("str", str);
        map.put("sum", sum);
//        String sum = str +"="+arr;
//        System.out.println("sum"+sum);
        return map;
    }

    @Override
    public Occupation countPerson() {
        return baseMapper.countPerson();
    }
}
